home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / TECHNICA / COMPUTER / H254.ZIP / IRITSM3S.ZIP / CAGD_LIB / CAGD_WRT.C < prev    next >
C/C++ Source or Header  |  1991-12-22  |  6KB  |  212 lines

  1. /******************************************************************************
  2. * Cagd_Wrt.c - Generic Curve/Surface writing to files.                  *
  3. *******************************************************************************
  4. * Written by Gershon Elber, July. 90.                          *
  5. ******************************************************************************/
  6.  
  7. #ifdef USE_VARARGS
  8. #include <varargs.h>
  9. #else
  10. #include <stdarg.h>
  11. #endif /* USE_VARARGS */
  12.  
  13. #include <string.h>
  14. #include "cagd_loc.h"
  15.  
  16. static CagdPrintfFuncType RedirCagdPrintfFunc = NULL;
  17.  
  18. /*****************************************************************************
  19. * Same as fprintf but with indentation.                         *
  20. *****************************************************************************/
  21. void CagdSetCagdFprintf(CagdPrintfFuncType Func)
  22. {
  23.     RedirCagdPrintfFunc = Func;
  24. }
  25.  
  26. /*****************************************************************************
  27. * Same as fprintf but with indentation.                         *
  28. *****************************************************************************/
  29. #ifdef USE_VARARGS
  30. void _CagdFprintf(FILE *f, int Indent, char *va_alist, ...)
  31. {
  32.     static char AccumLine[LINE_LEN_LONG];
  33.     char *p, *Format, Line[LINE_LEN_LONG];
  34.     int i;
  35.     va_list ArgPtr;
  36.  
  37.     va_start(ArgPtr);
  38.     Format = va_arg(ArgPtr, char *);
  39. #else
  40. void _CagdFprintf(FILE *f, int Indent, char *Format, ...)
  41. {
  42.     static char AccumLine[LINE_LEN_LONG];
  43.     char *p, Line[LINE_LEN_LONG];
  44.     int i;
  45.     va_list ArgPtr;
  46.  
  47.     va_start(ArgPtr, Format);
  48. #endif /* USE_VARARGS */
  49.  
  50.     vsprintf(Line, Format, ArgPtr);
  51.     va_end(ArgPtr);
  52.  
  53.     if (RedirCagdPrintfFunc && f == NULL) {
  54.     for (i = 0, p = &AccumLine[strlen(AccumLine)];
  55.          i < Indent;
  56.          i++) *p++ = ' ';
  57.     *p = 0;
  58.  
  59.     if ((p = strchr(Line, '\n')) != NULL ||
  60.         (p = strchr(Line, '\r')) != NULL)
  61.         *p = 0;
  62.     strcat(AccumLine, Line);
  63.     if (p != NULL) {
  64.         RedirCagdPrintfFunc(AccumLine);
  65.         AccumLine[0] = 0;
  66.     }
  67.     }
  68.     else {
  69.     for (i = 0; i < Indent; i++) fputc(' ', f);
  70.     fputs(Line, f);
  71.     }
  72. }
  73.  
  74. /*****************************************************************************
  75. * Generic routine to write curve(s) to the given file.                 *
  76. ( If Comment is NULL, no comment is printed, if "" only internal coment.     *
  77. *****************************************************************************/
  78. int CagdCrvWriteToFile2(CagdCrvStruct *Crvs, FILE *f, int Indent, char *Comment,
  79.                                 char **ErrStr)
  80. {
  81.     int RetVal = TRUE;
  82.     CagdCrvStruct *NextCrv;
  83.  
  84.     for (; Crvs != NULL && RetVal; Crvs = Crvs -> Pnext) {
  85.     NextCrv = Crvs -> Pnext;      /* To make sure we dump one at a time. */
  86.     Crvs -> Pnext = NULL;
  87.  
  88.     switch(Crvs -> GType) {
  89.         case CAGD_CBEZIER_TYPE:
  90.         RetVal = BzrCrvWriteToFile2(Crvs, f, Indent, Comment, ErrStr);
  91.         break;
  92.         case CAGD_CBSPLINE_TYPE:
  93.         RetVal = BspCrvWriteToFile2(Crvs, f, Indent, Comment, ErrStr);
  94.         break;
  95.         case CAGD_CPOWER_TYPE:
  96.         FATAL_ERROR(CAGD_ERR_POWER_NO_SUPPORT);
  97.         return FALSE;
  98.         default:
  99.         FATAL_ERROR(CAGD_ERR_UNDEF_CRV);
  100.         return FALSE;
  101.     }
  102.  
  103.     Crvs -> Pnext = NextCrv;
  104.     }
  105.  
  106.     return RetVal;
  107. }
  108.  
  109. /*****************************************************************************
  110. * Generic routine to write surface(s) to the given file.             *
  111. ( If Comment is NULL, no comment is printed, if "" only internal coment.     *
  112. *****************************************************************************/
  113. int CagdSrfWriteToFile2(CagdSrfStruct *Srfs, FILE *f, int Indent, char *Comment,
  114.                                 char **ErrStr)
  115. {
  116.     int RetVal = TRUE;
  117.     CagdSrfStruct *NextSrf;
  118.  
  119.     for (; Srfs != NULL && RetVal; Srfs = Srfs -> Pnext) {
  120.     NextSrf = Srfs -> Pnext;      /* To make sure we dump one at a time. */
  121.     Srfs -> Pnext = NULL;
  122.  
  123.     switch (Srfs -> GType) {
  124.         case CAGD_SBEZIER_TYPE:
  125.         RetVal = BzrSrfWriteToFile2(Srfs, f, Indent, Comment, ErrStr);
  126.         break;
  127.         case CAGD_SBSPLINE_TYPE:
  128.         RetVal = BspSrfWriteToFile2(Srfs, f, Indent, Comment, ErrStr);
  129.         break;
  130.         case CAGD_SPOWER_TYPE:
  131.         FATAL_ERROR(CAGD_ERR_POWER_NO_SUPPORT);
  132.         return FALSE;
  133.         default:
  134.         FATAL_ERROR(CAGD_ERR_UNDEF_SRF);
  135.         return FALSE;
  136.     }
  137.  
  138.     Srfs -> Pnext = NextSrf;
  139.     }
  140.  
  141.     return RetVal;
  142. }
  143.  
  144. /*****************************************************************************
  145. * Generic routine to write curve(s) to the given file.                 *
  146. ( If Comment is NULL, no comment is printed, if "" only internal coment.     *
  147. *****************************************************************************/
  148. int CagdCrvWriteToFile(CagdCrvStruct *Crvs, char *FileName, int Indent,
  149.                          char *Comment, char **ErrStr)
  150. {
  151.     int RetVal = TRUE;
  152.     CagdCrvStruct *NextCrv;
  153.  
  154.     for (; Crvs != NULL && RetVal; Crvs = Crvs -> Pnext) {
  155.     NextCrv = Crvs -> Pnext;      /* To make sure we dump one at a time. */
  156.     Crvs -> Pnext = NULL;
  157.  
  158.     switch(Crvs -> GType) {
  159.         case CAGD_CBEZIER_TYPE:
  160.         RetVal = BzrCrvWriteToFile(Crvs, FileName, Indent, Comment, ErrStr);
  161.         break;
  162.         case CAGD_CBSPLINE_TYPE:
  163.         RetVal = BspCrvWriteToFile(Crvs, FileName, Indent, Comment, ErrStr);
  164.         break;
  165.         case CAGD_CPOWER_TYPE:
  166.         FATAL_ERROR(CAGD_ERR_POWER_NO_SUPPORT);
  167.         default:
  168.         FATAL_ERROR(CAGD_ERR_UNDEF_CRV);
  169.         return FALSE;
  170.     }
  171.  
  172.     Crvs -> Pnext = NextCrv;
  173.     }
  174.  
  175.     return RetVal;
  176. }
  177.  
  178. /*****************************************************************************
  179. * Generic routine to write surface(s) to the given file.             *
  180. ( If Comment is NULL, no comment is printed, if "" only internal coment.     *
  181. *****************************************************************************/
  182. int CagdSrfWriteToFile(CagdSrfStruct *Srfs, char *FileName, int Indent,
  183.                          char *Comment, char **ErrStr)
  184. {
  185.     int RetVal = TRUE;
  186.     CagdSrfStruct *NextSrf;
  187.  
  188.     for (; Srfs != NULL && RetVal; Srfs = Srfs -> Pnext) {
  189.     NextSrf = Srfs -> Pnext;      /* To make sure we dump one at a time. */
  190.     Srfs -> Pnext = NULL;
  191.  
  192.     switch (Srfs -> GType) {
  193.         case CAGD_SBEZIER_TYPE:
  194.         RetVal = BzrSrfWriteToFile(Srfs, FileName, Indent, Comment, ErrStr);
  195.         break;
  196.         case CAGD_SBSPLINE_TYPE:
  197.         RetVal = BspSrfWriteToFile(Srfs, FileName, Indent, Comment, ErrStr);
  198.         break;
  199.         case CAGD_SPOWER_TYPE:
  200.         FATAL_ERROR(CAGD_ERR_POWER_NO_SUPPORT);
  201.         return FALSE;
  202.         default:
  203.         FATAL_ERROR(CAGD_ERR_UNDEF_SRF);
  204.         return FALSE;
  205.     }
  206.  
  207.     Srfs -> Pnext = NextSrf;
  208.     }
  209.  
  210.     return RetVal;
  211. }
  212.